home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 24 aspnet applications / aspnetapplications / xyzhandler.vb < prev   
Encoding:
Text File  |  2002-03-18  |  3.7 KB  |  99 lines

  1. ' this class is a sample HTTP handler that accepts requests for a 
  2. ' resource named   table.xyz and displays the contents of the Biblio database
  3. ' table with that name. You can optionally pass a WHERE clause on the query
  4. ' string, as follows
  5. '        publishers.xyz?city='New York'
  6.  
  7. ' it is enabled by the following lines in web.config
  8. '<httpHandlers>
  9. '   <add verb="*" path="*.xyz" type="AspnetApplications.XyzHandler,AspnetApplications" />
  10. '</httpHandlers>
  11.  
  12. Imports System.Data.OleDb
  13.  
  14. Public Class XyzHandler
  15.     Implements IHttpHandler
  16.  
  17.     ' this method is called whenever an .xyz request is posted to the server
  18.  
  19.     Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
  20.         ' The name of the page is the table table.
  21.         Dim tableName As String = context.Request.Path
  22.         ' drop directory name, if any, and the extension.
  23.         tableName = System.IO.Path.GetFileNameWithoutExtension(tableName)
  24.         ' the query string is an optional WHERE clause
  25.         Dim whereClause As String = context.Request.QueryString.ToString
  26.  
  27.         ' Build the SQL query.
  28.         Dim sql As String = "SELECT * FROM " & tableName
  29.         If whereClause.Length > 0 Then sql &= " WHERE " & whereClause
  30.  
  31.         ' Send the table to the client to the client.
  32.         context.Response.Write("<HTML><BODY>")
  33.         context.Response.Write(MakeHtmlTable(sql, context))
  34.         context.Response.Write("</BODY></HTML>")
  35.     End Sub
  36.  
  37.     ' this method is part of the IHttpHandler interface
  38.  
  39.     Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
  40.         Get
  41.             Return True
  42.         End Get
  43.     End Property
  44.  
  45.     ' Perform an SQL query, return the result as an HTML table.
  46.  
  47.     Function MakeHtmlTable(ByVal sql As String, ByVal context As HttpContext) As String
  48.         Dim cn As New System.Data.OleDb.OleDbConnection(BiblioConnString)
  49.         Dim cmd As New System.Data.OleDb.OleDbCommand(sql, cn)
  50.         Dim dr As System.Data.OleDb.OleDbDataReader
  51.  
  52.         ' Use a StringBuilder to create the output.
  53.         Dim sb As New System.Text.StringBuilder(10240)
  54.  
  55.         Try
  56.             ' Open a connection to Biblio and process the query.
  57.             cn.Open()
  58.             dr = cmd.ExecuteReader
  59.  
  60.             ' Create an HTML table with correct header row.
  61.             sb.Append("<TABLE Border='1'><THEAD>")
  62.             Dim i As Integer
  63.             For i = 0 To dr.FieldCount - 1
  64.                 sb.Append("<TH>" & dr.GetName(i) & "</TH>")
  65.             Next
  66.             sb.Append("</THEAD>")
  67.  
  68.             ' Output data for each record.
  69.             Do While dr.Read
  70.                 sb.Append("<TR>")
  71.                 For i = 0 To dr.FieldCount - 1
  72.                     ' A single field value (must be encoded for html).
  73.                     sb.Append("<TD>")
  74.                     If Not dr.IsDBNull(i) Then
  75.                         sb.Append(context.Server.HtmlEncode(dr(i).ToString))
  76.                     Else
  77.                         sb.Append("(null)")
  78.                     End If
  79.                     sb.Append("</TD>")
  80.                 Next
  81.                 sb.Append("</TR>")
  82.             Loop
  83.             ' close the table .
  84.             sb.Append("</TABLE>")
  85.  
  86.         Catch ex As Exception
  87.             sb.Append("<h1>Unable to process the request</h1>")
  88.         Finally
  89.             ' Close the data reader and the connection, if necessary.
  90.             If Not (dr Is Nothing) AndAlso Not dr.IsClosed Then dr.Close()
  91.             If cn.State = ConnectionState.Open Then cn.Close()
  92.         End Try
  93.  
  94.         ' Return the HTML text to the caller.
  95.         Return sb.ToString
  96.     End Function
  97.  
  98. End Class
  99.